/* 
*  Copyright (C) 2009-2010 WCG Project
*
*  This program is free software; you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by the
*  Free Software Foundation; either version 2 of the License, or (at
*  your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but
*  WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software Foundation,
*  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*  In addition, as a special exception, the author gives permission to
*  link the code of this program with the Half-Life Game Engine ("HL
*  Engine") and Modified Game Libraries ("MODs") developed by Valve,
*  L.L.C ("Valve"). You must obey the GNU General Public License in all
*  respects for all of the code used other than the HL Engine and MODs
*  from Valve. If you modify this file, you may extend this exception
*  to your version of the file, but you are not obligated to do so. If
*  you do not wish to do so, delete this exception statement from your
*  version.
*/



//-------------------------------------------
// ResetAttributeLevels
//
// Function will reset all of the user's attribute levels
//-------------------------------------------
ResetAttributeLevels( id, iDebugID = 0 )
{
	if( !IsValidUser( id ) )
	{
		WC3_Log( false, "[ResetAttributeLevels] Invalid userID: %d [%d]", id, iDebugID )
		log_error( AMX_ERR_NATIVE, "[ResetAttributeLevels] Invalid userID: %d [%d]", id, iDebugID )
		return
	}

	for( new i = 0; i < MAX_ATTRIBUTES; i++ )
		g_PlayerAttributeLevel[id][i] = 0

	PlayerInfo[id][PI_STR] = 0
	PlayerInfo[id][PI_STA] = 0
	PlayerInfo[id][PI_DEX] = 0
	PlayerInfo[id][PI_AGI] = 0
	PlayerInfo[id][PI_INT] = 0
	PlayerInfo[id][PI_SPR] = 0
	PlayerInfo[id][PI_BaseHealth] = 100
}



//-------------------------------------------
// GetAttributeLevel
//
// Returns the user's level for a certain attribute
// The value is not cached with base class values
//-------------------------------------------
GetAttributeLevel( id, iAttribID )
{
	if( !IsValidUser( id ) )
	{
		WC3_Log( false, "[GetAttributeLevel] Invalid userID: %d", id )
		log_error( AMX_ERR_NATIVE, "[GetAttributeLevel] Invalid userID: %d", id )
		return 0
	}

	if( !IsValidAttribute( iAttribID ) )
	{
		WC3_Log( false, "[GetAttributeLevel] Invalid attribute: %d", iAttribID )
		log_error( AMX_ERR_NATIVE, "[GetAttributeLevel] Invalid attribute: %d", iAttribID )
		return 0
	}

	return g_PlayerAttributeLevel[id][iAttribID]
}


//-------------------------------------------
// SetAttributeLevel
//
// Set the user's level for a given attribute
//-------------------------------------------
SetAttributeLevel( id, iAttribID, iAttribLevel )
{
	if( !IsValidUser( id ) )
	{
		WC3_Log( false, "[SetAttributeLevel] Invalid userID: %d", id )
		log_error( AMX_ERR_NATIVE, "[SetAttributeLevel] Invalid userID: %d", id )
		return
	}

	if( !IsValidAttribute( iAttribID ) )
	{
		WC3_Log( false, "[SetAttributeLevel] Invalid attribute: %d", iAttribID )
		log_error( AMX_ERR_NATIVE, "[SetAttributeLevel] Invalid attribute: %d", iAttribID )
		return
	}

	// We shouldn't have a level greater than the max level
	if( iAttribLevel > MAX_ATTRIBUTELEVEL )
	{
		WC3_Log( false, "[SetAttributeLevel] Attibute level is higher then MAX_ATTRIBUTELEVEL: %d", iAttribLevel )
		log_error( AMX_ERR_NATIVE, "[SetAttributeLevel] Attibute level is higher then MAX_ATTRIBUTELEVEL: %d", iAttribLevel )
		return
	}

	iAttribLevel = clamp( iAttribLevel, 0, MAX_ATTRIBUTELEVEL )
	
	// Set our new skill value
	g_PlayerAttributeLevel[id][iAttribID] = iAttribLevel

	InitAttributeEffects( id )
}


//-------------------------------------------
// ValidateAttributes
//
// Checks if there is something wrong with the attribute values
// Returns true if everything is ok, else false
//-------------------------------------------
ValidateAttributes( id )
{
	if( !IsValidUser( id ) )
	{
		WC3_Log( false, "[ValidateAttributes] Invalid userID: %d", id )
		log_error( AMX_ERR_NATIVE, "[ValidateAttributes] Invalid userID: %d", id )
		return false
	}

	new iTotal = 0

	for( new i = 0; i < MAX_ATTRIBUTES; i++ )
	{
		if( g_PlayerAttributeLevel[id][i] < 0 || g_PlayerAttributeLevel[id][i] > MAX_ATTRIBUTELEVEL )
			return false

		iTotal += g_PlayerAttributeLevel[id][i]
	}
	
	if( iTotal > PlayerInfo[id][PI_Level] )
		return false
	
	return true
}


//-------------------------------------------
// GetAvailableAttributePoints
//
// Returns how many attribute points the player has available to spend
//-------------------------------------------
GetAvailableAttributePoints( id )
{
	if( !IsValidUser( id ) )
	{
		WC3_Log( false, "[GetAvailableAttributePoints] Invalid userID: %d", id )
		log_error( AMX_ERR_NATIVE, "[GetAvailableAttributePoints] Invalid userID: %d", id )
		return 0
	}

	static i, iTotal

	iTotal = 0

	for( i = 0; i < MAX_ATTRIBUTES; i++ )
		iTotal += g_PlayerAttributeLevel[id][i]

	iTotal = PlayerInfo[id][PI_Level] - iTotal

	// This shouldn't happen, but murphy's law dictates that it will.
	if( iTotal < 0 )
		iTotal = 0

	return iTotal
}


//-------------------------------------------
// IncreaseAttribute
//
// Increase attribute vaule by given value
//-------------------------------------------
IncreaseAttribute( id, iAttribID, iValue = 1 )
{
	SetAttributeLevel( id, iAttribID, g_PlayerAttributeLevel[id][iAttribID] + iValue )
}


//-------------------------------------------
// InitAttributeEffects
//
// Initiallizes effects caused by attribue settings
//-------------------------------------------
InitAttributeEffects( id )
{
	if( !IsValidUser( id ) )
	{
		WC3_Log( false, "[InitAttributeEffects] Invalid userID: %d", id )
		log_error( AMX_ERR_NATIVE, "[InitAttributeEffects] Invalid userID: %d", id )
		return
	}

	PlayerInfo[id][PI_STR] = g_PlayerAttributeLevel[id][ATTRIBUTE_STR] + PlayerBuffInfo[id][BUFF_BoK]
	PlayerInfo[id][PI_STA] = g_PlayerAttributeLevel[id][ATTRIBUTE_STA] + PlayerBuffInfo[id][BUFF_BoK]
	PlayerInfo[id][PI_DEX] = g_PlayerAttributeLevel[id][ATTRIBUTE_DEX] + PlayerBuffInfo[id][BUFF_BoK]
	PlayerInfo[id][PI_AGI] = g_PlayerAttributeLevel[id][ATTRIBUTE_AGI] + PlayerBuffInfo[id][BUFF_BoK]
	PlayerInfo[id][PI_INT] = g_PlayerAttributeLevel[id][ATTRIBUTE_INT] + PlayerBuffInfo[id][BUFF_BoK]
	PlayerInfo[id][PI_SPR] = g_PlayerAttributeLevel[id][ATTRIBUTE_SPR] + PlayerBuffInfo[id][BUFF_BoK]

	PlayerInfo[id][PI_BaseHealth]		= 100 + (PlayerInfo[id][PI_Level]/2) + (PlayerInfo[id][PI_STA] * 2)
	PlayerInfo[id][PI_AttackModifier]	= _:(1/((PlayerInfo[id][PI_Level]*2+PlayerInfo[id][PI_STR]*4+PlayerInfo[id][PI_AGI]*2)*0.002))
	PlayerInfo[id][PI_MaxMana]			= 100 + PlayerInfo[id][PI_Level] + PlayerInfo[id][PI_INT] * 4

	if( !is_user_alive(id) )
		return

	fm_set_user_health( id, PLAYER_GetMaxhealth(id) )

	PLAYER_UpdateSpeed( id )
}
